$(function () {

        var stock_datagrid = $("#stock_datagrid");
        var stock_dialog = $("#stock_dialog");
        var stock_form = $("#stock_form");
        var pro_datagrid = $("#pro_datagrid");

        //事件统一绑定
        $("[data-cmd]").click(function () {
            var cmd = $(this).data("cmd");
            methodObj[cmd]();
        });

        stock_dialog.dialog({
            width: 350,
            height: 350,
            buttons: "#stock_btns",
            closed: true,
            modal: true,
            onOpen: function () {
                $.get('/stock/updateData.do', function (data) {

                })
            },
        })


        stock_datagrid.datagrid({
            url: '/stock/list.do',
            fit: true,
            fitColumns: true,
            singleSelect: true,
            rownumbers: true,
            toolbar: '#stock_toolbar',
            columns: [
                [

                    {field: 'id', title: '序号', width: 100},
                    {field: 'productName', title: '商品名称', width: 100},
                    {field: 'productFirstlevel', title: '商品类别', width: 100},
                    {field: 'productSpecification', title: '单位', width: 100},
                    {field: 'stockNumber',title:'当前库存数量',width: 100},
                    {field: 'stockTime', title: '上次盘点时间', width: 100},
                    {field: 'remark', title: '备注', width: 100}
                ]
            ]

        });

        var methodObj = {
            add: function () {
                //判断是否有选中数据
                var row = stock_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                    return;
                }
                //清空表单数据
                stock_form.form("clear");
                // 将选中的行中的数据填入空格中
                stock_form.form("load",row);

                $("#edit_tbody").html("");
                //设置标题
                stock_dialog.dialog("setTitle", "审核数量");


                //打开弹窗
                stock_dialog.dialog("open");

            },
            save: function () {

                stock_form.form("submit", {
                    url: '/stock/updateData.do',
                    onSubmit : function(param){
                      param.newStockNumber='newStockNumber';
                    },
                    success: function (data) {
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info', function () {
                                //重新加载数据
                                stock_datagrid.datagrid("reload");
                                //关闭弹窗
                                stock_dialog.dialog("close");
                            });
                        } else {
                            $.messager.alert('温馨提示', '操作失败', 'error');
                        }
                    }




                });
            },
            closeT:function () {
                // 关闭窗口
                stock_dialog.dialog("close");
            },
            delestT:function () {
                // 删除
                //判断是否有选中数据
                var row = stock_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                    return;
                }
                var index = stock_datagrid.datagrid("getRowIndex",row);
                $.messager.confirm('温馨提示', '请选中一条数据!', function (r) {
                    if (r){
                        stock_datagrid.datagrid("deleteRow",index);
                    }
                });



            },

           reloadT: function () {
                 stock_datagrid.datagrid("reload");
             }


            /*  addProduct: function () {

                  $("#product_view").dialog("open");
              }*/
        }
        /* */

        /*  pro_datagrid.datagrid({
              url: '/product/listView.do',
              fit: true,
              fitColumns: true,
              pagination: true,
              singleSelect: true,
              rownumbers: true,
              toolbar: '#view_toolbar',
              columns: [[
                  {field: 'id', hidden: true},
                  {field: 'name', title: '商品名', width: 100},
                  {field: 'sn', title: '编码', width: 100},
                  {field: 'costPrice', title: '进价', width: 100},
                  {field: 'costPrice', title: '售价', width: 100},
                  {field: "imgPath", title: "商品图", width: 100, formatter: imgformatter},
                  {field: "remark", title: "备注", width: 100}
              ]],
              onClickRow: function (index, row) {
                  var copy = $("#itemTr tr:first").clone();
                  copy.appendTo("#edit_tbody");
                  copy.find("[tag=sn]").html(row.sn);
                  copy.find("[tag=snn]").val(row.sn);
                  copy.find("[tag=name]").html(row.name);
                  copy.find("[tag=pid]").val(row.id);
                  copy.find("[tag=costPrice]").val(row.costPrice);
                  $("#product_view").dialog("close");
              },

          });
         ;*/
        /*$("#product_view").dialog({
            width: 800,
            height: 500,
            closed: true,
            modal: true,
            title: '添加商品'
        });
        $("#edit_tbody").on("change", "[tag=costPrice],[tag=number]", function () {
            var currentTr = $(this).closest('tr');
            var price = currentTr.find("[tag=costPrice]").val() || 0;
            var number = currentTr.find("[tag=number]").val() || 0;
            currentTr.find("[tag=amount]").html((price * number).toFixed(2));
        }).on("click", ".deleteItem", function () {
            var currentTr = $(this).closest("tr");
            currentTr.remove();
        })*/
    }
)


